//
//  HomePageDetailTableViewCell.m
//  Taste
//
//  Created by 徐昊 on 16/9/18.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "HomePageDetailTableViewCell.h"
#import "ImageAndLabelCollectionViewCell.h"
#import "HomePageDetailMore.h"
#import "Latestcon.h"
#import "HXMacro.h"
#import "HomePageDetailViewController.h"
#import "MasterCookBookViewController.h"

#define kHomePageCollectionViewImageAndLabelCellName @"HomePageCollectionViewImageAndLabelCellReuse"
@interface HomePageDetailTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;

@end

@implementation HomePageDetailTableViewCell

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
    _layout.minimumInteritemSpacing = 5;
    _layout.minimumLineSpacing = 5;
    _layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView.scrollEnabled = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerNib:[UINib nibWithNibName:@"ImageAndLabelCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kHomePageCollectionViewImageAndLabelCellName];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger num = 0;
    if (_more) {
        num = _more.count;
    } else if (_latestCon) {
        num = _latestCon.count;
    }
    return num;
}
- (void)setLatestCon:(NSMutableArray<Latestcon *> *)latestCon {
    _latestCon = latestCon;
    [_collectionView reloadData];
}
- (void)setMore:(NSMutableArray<HomePageDetailMore *> *)more {
    _more = more;
    [_collectionView reloadData];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ImageAndLabelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHomePageCollectionViewImageAndLabelCellName forIndexPath:indexPath];
    if (_latestCon) {
        cell.latestCon = _latestCon[indexPath.row];
        cell.label.numberOfLines = 2;
        cell.label.font = [UIFont systemFontOfSize:15];
    } else if (_more) {
        cell.more = _more[indexPath.row];
        cell.label.numberOfLines = 2;
        cell.label.font = [UIFont systemFontOfSize:15];
    }
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 20) / 3, (SCREEN_WIDTH - 20) / 3 + 40);
    return _layout.itemSize;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HomePageDetailViewController *vc = [[HomePageDetailViewController alloc] init];
    MasterCookBookViewController *masterCookVC = [[MasterCookBookViewController alloc] init];
    HomePageDetailViewController *homeVC = (HomePageDetailViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
    if (_more) {
        vc.numString = _more[indexPath.row].HomePageDetailMore_id;
        [homeVC.navigationController pushViewController:vc animated:YES];
    }
    if (_latestCon) {
        if ([_latestCon[indexPath.row].cid isEqualToString:@"1"]) {
            vc.numString = _latestCon[indexPath.row].LatestCon_id;
            [homeVC.navigationController pushViewController:vc animated:YES];
        }
        if ([_latestCon[indexPath.row].cid isEqualToString:@"14"]) {
            masterCookVC.numString = _latestCon[indexPath.row].LatestCon_id;
            [homeVC.navigationController pushViewController:masterCookVC animated:YES];
        }
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
